﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAO;
using BUS;

namespace FDeal.WUC
{
    public partial class WUC_DN_DangTin : System.Web.UI.UserControl
    {
        protected LoaiDealBUS _LoaiDealBUS;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                _LoaiDealBUS = new LoaiDealBUS();

                List<String> _LoaiDeal = new List<string>();
                List<LOAIDEAL> _listLoaiDealDAO = new List<LOAIDEAL>();
                _listLoaiDealDAO = _LoaiDealBUS.LietKe();

                for (int i = 0; i < _listLoaiDealDAO.Count; i++)
                {
                    _LoaiDeal.Add(_listLoaiDealDAO[i].TENLOAI);
                }

                ddl_LoaiDeal.DataSource = _LoaiDeal;
                ddl_LoaiDeal.DataBind();
            }
        }

        protected void btn_DangTin_Click(object sender, EventArgs e)
        {
            // get thong tin
            CHITIETDEAL tChiTietDeal = new CHITIETDEAL();

            tChiTietDeal.TIEUDE = txt_TieuDe.Text.Trim();
            tChiTietDeal.LOAIDEAL = _LoaiDealBUS.TimTen(ddl_LoaiDeal.SelectedValue).IDLOAIDEAL;
            tChiTietDeal.TOMTATDEAL = txt_TomTat.Text.Trim();
            tChiTietDeal.GIAGOC = float.Parse(txt_GiaGoc.Text);
            tChiTietDeal.DONVI = ddl_DonViTinh.SelectedValue;
            tChiTietDeal.GIAM = int.Parse(txt_Giam.Text);
            tChiTietDeal.GIACONLAI = tChiTietDeal.GIAGOC - tChiTietDeal.GIAM;
            tChiTietDeal.SODEALBAN = int.Parse(txt_SoLuong.Text);
            tChiTietDeal.THOIGIANCONLAI = int.Parse(txt_ThoiGian.Text);
            tChiTietDeal.HINHANH = "";
            tChiTietDeal.DIEUKIEN = txt_DieuKien.Text.Trim();
            tChiTietDeal.DIEMNOIBAT = txt_DiemNoiBat.Text.Trim();
            tChiTietDeal.DIACHI = txt_DiaChi.Text.Trim();
            tChiTietDeal.DIADIEMBANDEAL = txt_DiaDiemBan.Text.Trim();
            tChiTietDeal.NGUOIDANG = txt_NguoiDang.Text.Trim();

            tChiTietDeal.SONGUOIMUA = 0;
            tChiTietDeal.LUOTXEM = 0;
            tChiTietDeal.NGAYDANG = DateTime.Now;

            // thêm vào database
            new BUS.ChiTietDealBUS().Them(tChiTietDeal);
        }
    }
}